$(function(){
	var pagePath = $("#pagePath").val();
	
	//任务列表加载
	$('#trackTable').initTable({
        url:pagePath+"/cleanTask/cleanPipeLineTrack/getDataList.json",
        columnData:[
            {title:"任务名称",name:"TASKNAME",width:"10%",align:"center"},
            {title:"管道名称",name:"LINENAME",width:"15%",align:"center"},
            {title:"发球点",name:"FROMPIG",width:"10%",align:"center"},
            {title:"收球点",name:"TOPIG",width:"10%",align:"center"},
            {title:"计划清管时间",name:"PLANPAGDATETIME",width:"15%",align:"center"},
            {title:"创建人",name:"CREATEUSER",width:"10%",align:"center"},
            {title:"状态",name:"STAUS",width:"10%",align:"center",format:function(json){ return tableOpt.getState(json.STAUS)}},
            {title:"查看/附件",name:"ID",width:"15%",align:"center",format:function(json){return tableOpt.getOpt(json.ID)}}
        ],
        selectDataNow:true,
        afterLoadTable:addStateCss,
    });
	
});

function addStateCss(){
	var t = $("#trackTable").find("tr[dname='dataRow_']");
	for (var i = 0; i < t.length; i++) {
		var tdrow = t.eq(i).find("td");
		for (var j = 0; j < tdrow.length; j++) {
			if(tdrow.eq(j).children().html() == "已发布"){
				t.eq(i).find("td").css({'border-bottom': '1px solid #f4daa3','background-color':'#fffaf0'});
			}
		}
	}
}

//查询
function searchFun(){
	var pipe = $("#lineDiv").children("a").attr("id") == "gdmc" ? "" : $("#lineDiv").children("a").attr("id");
	var segment = $("#segmentDiv").children("a").attr("id") == "jcgdmc" ? "" : $("#segmentDiv").children("a").attr("id");
	var pigType = $("#toolDiv").children("a").attr("id") == "qggj" ? "" : $("#toolDiv").children("a").attr("id")
	var begTime = $("#begTime").val();
	var endTime = $("#endTime").val();
	var data = {"taskName":$.trim($("#taskName").val()),"lineId":pipe,"segmentId":segment,"begTime":begTime,"endTime":endTime,"pigType":pigType};
	$('#trackTable').selectDataAfterSetParm(data);
}

var tableOpt = {
		getState:function(data){
			if("2" == data){
				return "<p style='color:#FFAD00 !important'>已发布</p>";
			}else if("3" == data){
				return "进行中";
			}
		},
		
		getOpt:function(data){
			var html = "<li style='list-style-type:none;' class='w15 no-right'><a href='javascript:void(0);'  class='jk_00' onclick='getDetail(\""+data+"\");'></a><a href='javascript:void(0);' class='download_00' onclick='downloadTaskFile(\""+data+"\");'></a></li>";
			return html;
		}
}

/* 日期格式化 */
Date.prototype.Format = function(fmt) {
    var o = {
        "M+" : this.getMonth() + 1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
    for ( var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1,
                    (RegExp.$1.length == 1) ? (o[k])
                            : (("00" + o[k])
                            .substr(("" + o[k]).length)));
    return fmt;
}

function getDetail(obj){
	var pagePath = $("#pagePath").val();
	var taskId = obj;
	if(null == taskId || "" == taskId || undefined == taskId ){
		alert("操作失败，请刷新页面后重试");
		return ;
	}else{
		window.location.href = pagePath+"/cleanTask/cleanPipeLineTrack/getTrackDetail.html?taskId="+taskId;
	}
}

//获取检测管段
function getInspSegment(lineId){
	$("#inspSegment").html('');
	var pagePath = $("#pagePath").val();
	var url = pagePath + "/cleanTask/cleanPipeLineManager/getInspSegment.json";
	$().invoke(url,{"lineId":lineId},[function(m,r){
		var inspSegment = r;
		var html='';
		$.each(inspSegment,function(i,evalue){
			html += "<li><a href='javascript:void(0);' id='"+ evalue.id+"'>"+ evalue.name+"</a></li>";
		});
		$("#segmentDiv").html("<a href='javascript:void(0);' id='jcgdmc'>管段名称</a><span></span>");
		$("#inspSegment").html(html);
	}]);
}
//重置task 选项
function resetTask()
{
	var baseSelect = $(".selectTask").parent().find(".drop-list-min a");
	baseSelect.html("检测任务名称");
	baseSelect.removeAttr("id");
	$(".selectTask").empty();
}

//导出追踪点
function exportTrackPoint(){
	$("#exportForm").submit();
	
}
//导入跟踪点
function fileUpload(){
	var pagePath = $("#pagePath").val();
	var url = pagePath+"/cleanTask/cleanPipeLineTrack/importTrackPoint.json";
	//任务uId
	var taskId = $("#trackTaskId").val();
	$.ajaxFileUpload({
		url:url,
		fileElementId:"importFile",
		dataType:"json",
		secureuri:false,
		data:{"taskId":taskId},
		success:function(data){
			var msgCode = data.msgCode;
			if(msgCode =="2"){
				alert("请选择excel格式文件");
			}else if(msgCode == "3"){
				alert("所选文件过大，请重新选择");
			}else if(msgCode == "taskError"){
				alert("导入文件与任务关联不匹配，请重新选择");
			}else if(msgCode == "4"){
				alert("上传文件与原导出文件不匹配");
			}else if(msgCode == "5"){
				alert("上传文件与原导出文件不匹配");
			}else if(msgCode == "6"){
				alert("不允许修改文件ID的值");
			}else if(msgCode == "7"){
				alert("请填写有效的通过时间,格式为年/月/日 时:分:秒(xxxx/xx/xx 00:00:00)");
			}else if(msgCode == "cellNull"){
				alert("excel数据出现空值，请检查。");
			}else if(data.msgcode == "successful"){
				alert("操作成功");
//				window.location.href = pagePath + "/insp/inspTaskTrack/getInspTaskTrackIndex.html";
			}
		}
	});
}

function saveTaskTrack(){
	var saveflag = window.confirm("请确保跟踪点已经导入，确定要保存吗?");
	if(saveflag){
		var pagePath = $("#pagePath").val();
		var url = pagePath+"/cleanTask/cleanPipeLineTrack/saveTaskTrack.json";
		var taskId = $("#trackTaskId").val();
		$().invoke(url,{"taskId":taskId},[function(m,r){
			var code = r;
			if(code != null && code != ""){
				alert("保存成功");
				window.location.href = pagePath + "/cleanTask/cleanPipeLineTrack/getCleanPipeLineTrackIndex.html";
			}
		}]);
	}
}

//检测任务监控
/**
 * 页面初始化时需传数据格式。piggingstructure:收发球的json数组 ，marker：通过点的gisId
 * [{typeName:"piggingstructure",gisIdArray:[12,13,14]},{typeName:"marker",gisIdArray:[18,19,20]}]
 */
function monitorFun(obj){
	var taskId = obj;
	$("#monitorTaskId").val(taskId);
//	$("#monitorTaskName").html("任务名为：【 "+$("#"+taskId).find("li")[0].innerText + " 】的监控");
	var pagePath = $("#pagePath").val();
	window.location.href = pagePath + "/cleanTask/cleanPipeLineTrack/monitorFun.html?taskId="+taskId;
}

function startMonitor(){
	var taskId = $("#monitorTaskId").val();
	alert(taskId);
}
//显示发球弹出框
function showStartPig(){
	var pigNum = $("#trackFromPig").val();
	var pagePath = $("#pagePath").val();
	var url = pagePath+"/cleanTask/cleanPipeLineTrack/getPigByPigNumber.json";
	if(undefined != pigNum || "" != pigNum || null != pigNum){
		$().invoke(url,{"pigNum":pigNum},[function(m,r){
			var pig = r.pig;
			if(undefined != pig && null != pig){
				$("#startPigNum").val(pig.PIGNUM);
				$("#startPigMile").val(pig.MILEAGE);
				$("#startPigRef").val(pig.REFERENCE);
				$("#startPigRefDistance").val(pig.REFDISTANCE);
				//显示发球弹出框
				$("#startPig").modal("show");
			}
		
		}]);
	}
}
//上传发球照片
function uploadFileFrom(){
	$("#fileFrom").trigger("click");
}
//点击发球事件
function sendPigging(){
	var taskId = $("#trackTaskId").val();
	
}



function noPermitInput(e,id){    
    var evt = window.event || e ;  
     if(isIE()){  
         evt.returnValue=false; //ie 禁止键盘输入  
         if(id == "begTime"){
        	 $("#begTime").val("");
         }
         if(id == "endTime"){
        	 $("#endTime").val("");
         }
         if(id == "begPigTime"){
        	 $("#begPigTime").val("");
         }
     }else{  
         evt.preventDefault(); //fire fox 禁止键盘输入  
         if(id == "begTime"){
        	 $("#begTime").val("");
         }
         if(id == "endTime"){
        	 $("#endTime").val("");
         }
         if(id == "begPigTime"){
        	 $("#begPigTime").val("");
         }
     }     
}  
function isIE() {  
 if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)  
     return true;  
 else  
     return false;  
}   


/**
 * 下载任务附件
 */
function downloadTaskFile(taskId){
	var pagePath = $("#pagePath").val();
	var url = pagePath + "/cleanTask/cleanPipeLineManager/downloadTaskFile.json?taskId="+taskId;
	backgroundExport(url);
}